package cn.net.mugui.net.pc.panel;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Set;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

import com.mugui.Dui.DButton;
import com.mugui.Dui.DOptionPanel;
import com.mugui.Dui.DPanel;
import com.mugui.Dui.DTextField;
import com.mugui.base.base.Autowired;
import com.mugui.base.base.Component;
import com.mugui.base.client.net.bagsend.BagSend;
import com.mugui.base.client.net.bean.Message;
import com.mugui.tool.FileTool;

import cn.net.mugui.net.conf.SysConf;
import cn.net.mugui.net.pc.AppFrame;
import cn.net.mugui.net.pc.manager.FunctionUI;
import cn.net.mugui.net.pc.manager.FunctionUIManager;
import cn.net.mugui.net.user.SelfUtil;

@Component
public class LoginPanel extends DPanel {
	private CardLayout cardLayout = null;
	private JPanel panel_1 = null;

	public LoginPanel() {
		setBackground(null);
		setForeground(null);
		setLayout(new BorderLayout(0, 0));
		panel_1 = new JPanel();
		panel_1.setForeground(Color.DARK_GRAY);
		panel_1.setBackground(Color.DARK_GRAY);
		add(panel_1, BorderLayout.CENTER);
		cardLayout = new CardLayout(0, 0);
		panel_1.setLayout(cardLayout);

		JPanel panel_2 = new JPanel();
		panel_2.setForeground(Color.DARK_GRAY);
		panel_1.add(panel_2, "登录");

		JPanel panel_6 = new JPanel();

		JLabel label_3 = new JLabel("密码/私钥");
		label_3.setHorizontalAlignment(SwingConstants.RIGHT);
		label_3.setFont(new Font("宋体", Font.BOLD, 20));

		passwd = new JPasswordField();
		passwd.setColumns(128);
		passwd.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					// 登录
					login();
				}
			}
		});

		JPanel panel_3 = new JPanel();
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(gl_panel_2.createParallelGroup(Alignment.TRAILING).addGroup(gl_panel_2
				.createSequentialGroup().addContainerGap()
				.addGroup(gl_panel_2.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
						.addComponent(panel_6, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 615, Short.MAX_VALUE)
						.addGroup(Alignment.LEADING,
								gl_panel_2.createSequentialGroup()
										.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 119,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(passwd, GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)))
				.addContainerGap()));
		gl_panel_2.setVerticalGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup().addGap(30)
						.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
								.addComponent(passwd, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
								.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_3, GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE).addContainerGap()));

		JLabel label = new JLabel("可使用密码登录或私钥恢复");
		label.setFont(new Font("宋体", Font.BOLD, 16));
		label.setForeground(Color.RED);
		panel_3.add(label);
		panel_6.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		DButton button = new DButton((String) null, Color.white);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 登录

				login();
			}
		});
		button.setFont(new Font("宋体", Font.BOLD, 25));
		button.setText("登入");
		panel_6.add(button);
		panel_2.setLayout(gl_panel_2);

		JPanel panel = new JPanel();
		panel.setBackground(Color.LIGHT_GRAY);
		add(panel, BorderLayout.NORTH);
		panel.setLayout(new GridLayout(1, 2, 0, 0));

		JLabel label_1 = new JLabel("创建或恢复");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.addMouseListener(l);
		label_1.setBackground(Color.GREEN);
		label_1.setFont(new Font("宋体", Font.BOLD, 35));
		label_1.setForeground(Color.BLACK);
		panel.add(label_1);
		cardLayout.show(panel_1, "登录");
	}

	@Autowired
	private BagSend bagSend;

	@Autowired
	private SelfUtil selfUtil;

	protected void login() {
		DOptionPanel.setDSize(400, 120);
		Message message = selfUtil.createUser(new String(passwd.getPassword()));
		passwd.setText("");
		if (message.getType() == Message.ERROR) {
			DOptionPanel.showMessageDialog(this, message.getMsg(), "警告", DOptionPanel.OPTION_OK);
		} else {
			switch (message.getType()) {
			case SelfUtil.MESSAGE_TYPE_2:
			case SelfUtil.MESSAGE_TYPE_3:
				DOptionPanel.showMessageDialog(this, selfUtil.getPwd(), message.getMsg() + " 以下为登录密码",
						DOptionPanel.OPTION_OK);
				break;
			default:
				DOptionPanel.showMessageDialog(this, selfUtil.getPrivateKey(), message.getMsg() + " 以下为私钥可用于找回",
						DOptionPanel.OPTION_OK);
				break;
			}
			Set<Entry<String, FunctionUI>> entrySet = manager.entrySet();
			for (Entry<String, FunctionUI> entry : entrySet) {
				frame.addFunctionUI(entry.getValue());
			}
			frame.updateUI(manager.get(ChatPanel.class.getName()));
			manager.get(ChatPanel.class.getName()).dataInit();
		}
	}

	@Autowired
	private FunctionUIManager manager;

	private MouseListener l = new MouseListener() {

		@Override
		public void mouseReleased(MouseEvent e) {
		}

		@Override
		public void mousePressed(MouseEvent e) {
		}

		@Override
		public void mouseExited(MouseEvent e) {
			JLabel j = (JLabel) (e.getSource());
			int i = j.getBackground().getRGB() - 4000;
			j.setBackground(new Color(i));
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			JLabel j = (JLabel) (e.getSource());
			int i = j.getBackground().getRGB() + 4000;
			j.setBackground(new Color(i));

		}

		@Override
		public void mouseClicked(MouseEvent e) {
			JLabel j = (JLabel) (e.getSource());
			cardLayout.show(panel_1, j.getText());
		}
	};

	public void setCardLayout(String cardname) {
		cardLayout.show(panel_1, cardname);
	}

	private DTextField mail = null;
	private JPasswordField passwd = null;

	/**
	 * 
	 */
	private static final long serialVersionUID = 6552809899881312249L;

	@Autowired
	private AppFrame frame;

	@Autowired
	private SysConf sysConf;

	@Override
	public void init() {
		frame.updateTitle("等待");
		passwd.setText("");
		frame.setSize(450, 300);
		repaint();
	}

	private String getSaveUser() {
		File f = new File(FileTool.getWindowsPath() + "\\1");
		BufferedReader br = null;
		try {
			if (!f.exists())
				f.createNewFile();
			br = new BufferedReader(new FileReader(f));
			String s = br.readLine();
			if (s == null || s.trim().equals("")) {
				return "";
			} else {
				return s.trim();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "";

	}

	@Override
	public void quit() {
	}

	@Override
	public void dataInit() {
		// TODO 自动生成的方法存根

	}

	@Override
	public void dataSave() {
		// TODO 自动生成的方法存根

	}
}
